package net.hn.hnms.biz.plan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.plan.domain.DimMiningFace;
import net.hn.hnms.biz.plan.mapper.DimMiningFaceMapper;
import net.hn.hnms.biz.plan.request.CooperativeMiningFacReq;
import net.hn.hnms.biz.plan.request.DimMiningFaceReq;
import net.hn.hnms.biz.plan.service.IDimMiningFaceService;
import net.hn.hnms.biz.plan.vo.DimMiningFaceVO;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhaoyang
 * 回采工作面管理服务层实现
 * @date 2022-08-01
 */

@Service
@DS("slave")
public class DimMiningFaceServiceImpl extends BaseService<DimMiningFace, DimMiningFaceVO>  implements IDimMiningFaceService {

    @Autowired
    private DimMiningFaceMapper dimMiningFaceMapper;

    /**
     * @param dimMiningFace
     * @param pageQuery       查询回采工作面列表
     */
    @Override
    public ResponsePage<DimMiningFaceVO> selectPageList(DimMiningFaceReq dimMiningFace, PageQuery pageQuery) {
        Page<DimMiningFace> page = dimMiningFaceMapper.selectPage(pageQuery.build(), buildQuery(dimMiningFace).orderByDesc(DimMiningFace::getCreateTime));
        List<DimMiningFaceVO> list = page.getRecords().stream().map(o -> convert(o, DimMiningFaceVO.class)).collect(Collectors.toList());
        return new ResponsePage<>(list, page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<DimMiningFace> 查询条件
     */
    private LambdaQueryWrapper<DimMiningFace> buildQuery(DimMiningFaceReq req){
        if(req.getMineCode()==null&&req.getCompanyCode()==null){
            return new LambdaQueryWrapper<DimMiningFace>()
                    .like(StrUtil.isNotBlank(req.getMiningFaceCode()), DimMiningFace::getMiningFaceCode, req.getMiningFaceCode())
                    .like(StrUtil.isNotBlank(req.getMiningFaceName()), DimMiningFace::getMiningFaceName, req.getMiningFaceName())
                    .eq(StrUtil.isNotBlank(req.getStatus()), DimMiningFace::getStatus, req.getStatus())
                    .eq(StrUtil.isNotBlank(req.getIsKeyArea()), DimMiningFace::getIsKeyArea, req.getIsKeyArea())
//                .eq(StrUtil.isNotBlank(req.getMineCode()), DimMiningFace::getMineCode, req.getMineCode())
                    .ge(req.getBeginDateTime() != null, DimMiningFace::getCreateTime, req.getBeginDateTime())
                    .le(req.getEndDateTime() != null, DimMiningFace::getCreateTime, req.getEndDateTime())
//                    .and(o->o.eq(req.getMineCode()!=null,DimMiningFace::getMineCode, req.getMineCode()).or().eq(req.getCompanyCode()!=null, DimMiningFace::getCompanyCode, req.getCompanyCode()))
                    ;
        }else{
            return new LambdaQueryWrapper<DimMiningFace>()
                    .like(StrUtil.isNotBlank(req.getMiningFaceCode()), DimMiningFace::getMiningFaceCode, req.getMiningFaceCode())
                    .like(StrUtil.isNotBlank(req.getMiningFaceName()), DimMiningFace::getMiningFaceName, req.getMiningFaceName())
                    .eq(StrUtil.isNotBlank(req.getStatus()), DimMiningFace::getStatus, req.getStatus())
                    .eq(StrUtil.isNotBlank(req.getIsKeyArea()), DimMiningFace::getIsKeyArea, req.getIsKeyArea())

//                .eq(StrUtil.isNotBlank(req.getMineCode()), DimMiningFace::getMineCode, req.getMineCode())
                    .ge(req.getBeginDateTime() != null, DimMiningFace::getCreateTime, req.getBeginDateTime())
                    .le(req.getEndDateTime() != null, DimMiningFace::getCreateTime, req.getEndDateTime())
                    .and(o->o.eq(req.getMineCode()!=null,DimMiningFace::getMineCode, req.getMineCode()).or().eq(req.getCompanyCode()!=null, DimMiningFace::getCompanyCode, req.getCompanyCode()))
                    ;
        }

    }

    /**
     *
     * @param dimMiningFace
     * 查询开拓计划列表
     */
    @Override
    public List<DimMiningFaceVO> selectList(DimMiningFaceReq dimMiningFace) {
        List<DimMiningFace> list = dimMiningFaceMapper.selectList(buildQuery(dimMiningFace));
        return list.stream().map(o -> convert(o, DimMiningFaceVO.class)).collect(Collectors.toList());
    }

    /**
     * @param dimMiningFace 新增回采工作面
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DimMiningFaceReq dimMiningFace) {
        QueryWrapper<DimMiningFace> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mining_face_code", dimMiningFace.getMiningFaceCode());

        if (dimMiningFaceMapper.selectCount(queryWrapper) > 0) {
            throw new HuaNengException(Constants.FAIL, "已存在相同的工作面编号");
        }
        DimMiningFace insertObj = new DimMiningFace();
        BeanUtil.copyProperties(dimMiningFace, insertObj);
        insertObj.setStatus("0");

        return dimMiningFaceMapper.insert(insertObj);
    }

    /**
     * @param idList 批量删除 id集合
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<String> idList) {
        dimMiningFaceMapper.deleteBatchIds(idList);
    }

    /**
     * @param dimMiningFace 单个修改 按照主键修改信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(DimMiningFaceReq dimMiningFace) {
        if (StrUtil.isBlank(dimMiningFace.getMiningFaceId())) {
            throw new HuaNengException(Constants.FAIL, "id不能为空");
        }
        DimMiningFace updateObj = new DimMiningFace();
        BeanUtil.copyProperties(dimMiningFace, updateObj);
        int updateCount = dimMiningFaceMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    /**
     * @param id 按主键id查询详情
     */
    @Override
    public DimMiningFaceVO selectDetailById(String id) {
        DimMiningFace DimMiningFace = dimMiningFaceMapper.selectById(id);
        if (DimMiningFace == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return convertWithUser(DimMiningFace, DimMiningFaceVO.class);
    }


    /**
     * 启用/禁用
     * @param id 数据id
     * @param status  status: 0:启用 1:禁用
     */
    public void changeStatus(String id, Integer status) {
        if (status == 1 || status == 0) {
            DimMiningFace planMining = dimMiningFaceMapper.selectById(id);
            if (planMining == null) {
                throw new HuaNengException(Constants.FAIL, "数据不存在");
            }
            planMining.setStatus(status + "");
            int res = dimMiningFaceMapper.updateById(planMining);
            if (res == 0) {
                throw new HuaNengException(Constants.FAIL, "数据不存在");
            }
            return;
        }
        throw new HuaNengException(Constants.FAIL, "传入的参数错误");
    }

    /**
     *
     * @param req
     * @return
     */
    public DimMiningFace getDimMiningFace(CooperativeMiningFacReq req) {
        if (req == null) {
            throw new HuaNengException(500, "参数为空");
        }
        if (StringUtils.isAllBlank(req.getId(), req.getName())) {
            throw new HuaNengException(500, "参数为空");
        }
        LambdaUpdateWrapper<DimMiningFace> wrapper = new LambdaUpdateWrapper<>();
        wrapper
                .eq(DimMiningFace::getStatus,"0")
                .eq(StrUtil.isNotBlank(req.getId()), DimMiningFace::getMiningFaceId, req.getId())
                .eq(StrUtil.isNotBlank(req.getMineCode()), DimMiningFace::getMineCode, req.getMineCode());
        if (req.getId()==null){
            wrapper.eq(StrUtil.isNotBlank(req.getName()), DimMiningFace::getMiningFaceName, req.getName());
        }
        DimMiningFace dimMiningFace = dimMiningFaceMapper.selectOne(wrapper);
        return dimMiningFace;
    }

    @Override
    public DimMiningFaceVO getWorkingFaceId(String name) {
        DimMiningFaceVO v= dimMiningFaceMapper.getWorkingFaceId(name);
        return v;
    }
}
